#!/usr/bin/python3
#coding: utf-8
import telepot, subprocess, re
import json, requests

#Handler
def handle(msg):

    # Reply function
    def response(id, response):
        bot.sendMessage(id, response, reply_to_message_id=chat_msgid, parse_mode='Markdown')


    content_type, chat_type, chat_id = telepot.glance(msg)
    message_id                       = msg['message_id']
    chat_adm						 = bot.getChatAdministrators(-1001319681290)

    try:
        print(content_type, chat_type, chat_id)
    except:pass;

    try:
        message_id  = msg['message_id']
        user        = msg['from']['first_name'] # Get the user who send the msg
        chat_id     = msg['chat']['id']
        message     = msg['text'].split()       # Message parse the message into elements
        message2    = msg['text'] 				# message2 its the raw text
        command     = re.sub('\@rabb1t_bot$', '', message[0])
        id          = msg['from']['id']
        commands    = ['/id']

    except Exception as e:
    	print('Error: {0}'.format(e));pass

    if content_type == ('text'):

    	git_url = re.findall(r'(https?://github.com[^\s]+)', message2)

    	if git_url:
        	git_url_api = ''.join(git_url).replace('https://github.com/','https://api.github.com/repos/')
        	r 			= requests.get(git_url_api).text
        	r 			= json.loads(r)

        	if chat_id == -1001319681290:
        		bot.sendMessage(-1001277022755, '`{}`\n\n{}'.format(r['description'],''.join(git_url)), parse_mode='Markdown')
        		bot.sendMessage(chat_id, '[+] Enviado para o canal!', reply_to_message_id=message_id, parse_mode='Markdown')

    if content_type == ('document'):

        file_name   = msg['document']['file_name']    # Get the original sender file name
        file_id     = msg['document']['file_id']      # Get the file id to download
        dest        = ('/home/t1/Desktop/') 		  # Path to download

        if file_name.lower().endswith(('.pdf', '.doc', '.txt', '.docx', '.epub')) == 1:

            try:

                if chat_id != -1001277022755:
                    bot.sendMessage(chat_id, '[+] Enviado para o canal!', reply_to_message_id=message_id, parse_mode='Markdown')
                    bot.forwardMessage(-1001277022755, chat_id, message_id)
            except Exception as e:
                print('Donwload document error: {0}'.format(e));pass


# API + Start
bot = telepot.Bot('1236729242:AAH6UlxlCJswpFPhs2NK1ff5DinxF7xcDL8') # Telegram API token
bot.message_loop(handle);print('Online!')

# Keep the bot running
while 1:pass
